
package com.kugou.game.sdk.core;

import com.kugou.game.sdk.api.common.IEventCode;
import com.kugou.game.sdk.api.common.IEventDataField;
import com.kugou.game.sdk.api.common.OnPlatformEventListener;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.statistics.StaticsManager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

/**
 * 描述:SDK事件分发器
 * 
 * @author chenys
 * @since 2014-11-27 上午11:43:13
 */
public final class PlatformEventDispatcher {

    private static final int MSG_UI_SEND_EVENT = 1;

    protected static Handler mHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_UI_SEND_EVENT:
                    try {
                        OnPlatformEventListener onPlatformEventListener = SDKControler
                                .getOnPlatformEventListener();
                        if (onPlatformEventListener != null) {
                            int eventCode = msg.arg1;
                            Bundle data = (Bundle) msg.obj;
                            if (data != null) {
                                User user = (User) data.getSerializable(IEventDataField.EXTRA_USER);
                                if (user != null) {
                                    User newUser = user.copySimpleUser();
                                    data.putSerializable(IEventDataField.EXTRA_USER, newUser);
                                }
                            }
                            onPlatformEventListener.onEventOccur(eventCode, data);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        };
    };

    /**
     * 发送事件.
     * 
     * @param eventCode
     * @param data
     */
    public static void sendEvent(int eventCode, Bundle data) {
        mHandler.obtainMessage(MSG_UI_SEND_EVENT, eventCode, 0, data).sendToTarget();
        // 发送统计-支付成功
        if (eventCode == IEventCode.RECHARGE_SUCCESS) {
            String orderid = "";
            int optype = 0, paytype = 0, paymoney = 0;
            if (data != null) {
                orderid = data.getString("orderid");
                optype = data.getInt("optype", 0);
                paytype = data.getInt("paytype", 0);
                paymoney = data.getInt("paymoney", 0);
            }
            StaticsManager.sendRechargeSuccessStatics(optype, paytype, paymoney, orderid);
        }
    }

    /**
     * 立即发送事件.
     * 
     * @param eventCode
     * @param data
     */
    public static void sendEventNow(int eventCode, Bundle data) {
        try {
            SDKControler.getOnPlatformEventListener().onEventOccur(eventCode, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 在主线程中运行
     * 
     * @param runnable
     */
    public static void runOnUiThread(Runnable runnable) {
        mHandler.post(runnable);
    }

    /**
     * 在主线程中运行
     * 
     * @param runnable
     */
    public static void runOnUiThreadDelayed(Runnable runnable, long delayMillis) {
        mHandler.postDelayed(runnable, delayMillis);
    }
}
